package cn.yunhe.control;

import cn.yunhe.entity.Admin;
import cn.yunhe.server.AdminServer;

import java.io.*;
import java.util.Map;

public class RegisterAction extends ActionBase {
    private Admin admin;
    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    private String savePath;

    @Override
    public String execute() throws Exception {
        AdminServer adminServer = new AdminServer();
        boolean flag = adminServer.register(admin.getAname(), admin.getApass());
        String path = getSavePath()+"\\"+getUploadFileName(); //获取完整路径
        doUpload(upload, path);
        Map request = getMapRequest();
        if (flag){
            request.put("result", "注册成功!!");
            return SUCCESS;
        }else {
            return ERROR;
        }
    }

    public void doUpload(File file, String path) throws IOException {
        FileInputStream fis = new FileInputStream(file); //添加上传的文件
        FileOutputStream fos = new FileOutputStream(path); //添加输出的路径
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        fis.close();
        fos.close();
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
}
